package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxUserPermissionDao;
import com.syni.mdd.yhd.common.all.entity.BmsDxUserPermission;
import com.syni.mdd.yhd.common.all.entity.permission.PermessionCodeUtils;
import com.syni.mdd.yhd.common.all.entity.permission.PermissionCode;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxUserPermissionService extends BaseService<BmsDxUserPermission, Integer> {

    private static Map<Integer,List<Integer>> userPermission = new HashMap<>();

    @Autowired
    BmsDxUserPermissionDao bmsDxUserPermissionDao;

    @Override
    public BmsDxUserPermissionDao getBaseDao() {
        return bmsDxUserPermissionDao;
    }

    public Map<Integer, List<Integer>> getUserRole(Set<Integer> bmsUserIds,Integer roleId){
        Map<Integer,List<Integer>> resultMap = new HashMap<>();
        if(bmsUserIds.size() > 0){
            List<BmsDxUserPermission> bmsDxUserPermissions = bmsDxUserPermissionDao.findByBmsUserIdInAndRoleId(bmsUserIds,roleId);
            if(bmsDxUserPermissions != null && bmsDxUserPermissions.size() > 0){
                for(BmsDxUserPermission bmsDxUserPermission : bmsDxUserPermissions){
                    Integer bmsUserId = bmsDxUserPermission.getBmsUserId();
                    if(resultMap.containsKey(bmsUserId)){
                        resultMap.get(bmsUserId).add(bmsDxUserPermission.getBmsPermissionId());
                    }else {
                        List<Integer> permissionIds = new LinkedList<>();
                        permissionIds.add(bmsDxUserPermission.getBmsPermissionId());
                        resultMap.put(bmsUserId,permissionIds);
                    }
                }
            }
        }
        return resultMap;
    }

    public void deleteUserPermission(Integer userId,Integer roleId){
        List<BmsDxUserPermission> bmsDxUserPermissions = bmsDxUserPermissionDao.findByBmsUserIdAndAndRoleId(userId,roleId);
        if(bmsDxUserPermissions != null && bmsDxUserPermissions.size() > 0){
            for(BmsDxUserPermission bmsDxUserPermission : bmsDxUserPermissions){
                bmsDxUserPermissionDao.delete(bmsDxUserPermission);
            }
        }
    }

    public Map<Integer, Integer> findUserHavingGroupPermission(Set<Integer> assistantIds) {
        Map<Integer,Integer> resultMap = new HashMap<>();
        List<BmsDxUserPermission> bmsDxUserPermissions = bmsDxUserPermissionDao.
                findByBmsUserIdInAndRoleIdAndBmsPermissionId(assistantIds,
                        Constants.ASSISTANT_ROLE_ID,Constants.GROUP_CHAT_PERMISSION);
        if(bmsDxUserPermissions != null && bmsDxUserPermissions.size() > 0){
            for(BmsDxUserPermission bmsDxUserPermission : bmsDxUserPermissions){
                resultMap.put(bmsDxUserPermission.getBmsUserId(),Constants.COMMON_FLAG_TRUE_INT);
            }
        }
        return resultMap;
    }

    public boolean checkUserPermission(Integer userId,Integer roleId,Integer code){
        List<BmsDxUserPermission> bmsDxUserPermissions = bmsDxUserPermissionDao.
                findByBmsUserIdAndRoleIdAndBmsPermissionId(userId,
                        roleId,code);
        if(bmsDxUserPermissions != null && bmsDxUserPermissions.size() > 0){
            return true;
        }
        return false;
    }

    public void insertDefaultAssistantPermission(Integer userId) {
        List<BmsDxUserPermission> bmsDxUserPermissions = bmsDxUserPermissionDao.findByBmsUserIdAndAndRoleId(userId,Constants.ASSISTANT_ROLE_ID);
        if(CollectionUtils.isNotEmpty(bmsDxUserPermissions)){
            bmsDxUserPermissionDao.deleteAll(bmsDxUserPermissions);
        }
        List<BmsDxUserPermission> permissions = new ArrayList<>();
        PermissionCode permissionCode = PermessionCodeUtils.permissionCodeMap.get(Constants.ASSISTANT_ROLE_ID);
        for(Integer code : permissionCode.getCodeList()){
            BmsDxUserPermission bmsDxUserPermission = new BmsDxUserPermission();
            bmsDxUserPermission.setBmsUserId(userId);
            bmsDxUserPermission.setBmsPermissionId(code);
            bmsDxUserPermission.setRoleId(Constants.ASSISTANT_ROLE_ID);
            permissions.add(bmsDxUserPermission);
        }
        if(permissions.size() > 0){
            bmsDxUserPermissionDao.saveAll(permissions);
        }
    }
}
